package com.mes.web.controller.yzl;


import com.mes.entity.yzl.Detail;
import com.mes.entity.yzl.ListBack;
import com.mes.entity.yzlExtend.ExtendDetail;
import com.mes.entity.yzlExtend.ExtendReceive;
import com.mes.service.yzl.DetailService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/yzl")
public class DetailController {

    protected Logger logger = Logger.getLogger(DetailController.class);
    private static final long serialVersionUID = 8550329576989690578L;

    @Resource
    public DetailService detailService;


    //查询
    @RequestMapping("/listdetail")
    public @ResponseBody ListBack listdetail(@RequestBody Map map){
        ExtendReceive extendReceive = new ExtendReceive();

        //参数
        extendReceive.setLimit((Integer)map.get("limit"));
        extendReceive.setOffset((Integer)map.get("offset"));
        extendReceive.setAssociatedIdStr((String)map.get("detailSupplier"));
        Detail detail = new Detail();
        detail.setDetailName((String)map.get("detailName"));
        detail.setDetailType((String)map.get("detailType"));
        extendReceive.setObject(detail);


        ListBack ListBack = new ListBack();

        logger.info("处理后的参数ExtendReceive--"+extendReceive);


        //条件查询总条数
        int count = detailService.countList(extendReceive);
        ListBack.setTotal(count);

        //分页查询
        List<Detail> list = detailService.listdetail(extendReceive);

        //查询所有
        //List<Detail> list = detailService.allList();

        ListBack.setRows(list);

        return ListBack;
    }


    //添加物料
    @RequestMapping("/addDetail")
    public @ResponseBody int addDetail(ExtendDetail extendDetail){
        //影响行数
        int isSuccess = 0;
        try{
            isSuccess = detailService.addDetail(extendDetail);
        }catch (Exception e){
            logger.info("addDetail()-- "+(new Date()).toLocaleString()+ e);
        }
        return isSuccess;

    }

    //修改物料信息
    @RequestMapping("/updateDetail")
    public @ResponseBody int updateDetail(Detail detail){

        ExtendReceive extendReceive = new ExtendReceive();

        int isSuccess = 0;
        logger.info("detail"+detail);

        extendReceive.setObject(detail);


        isSuccess = detailService.updateDetail(extendReceive);

        return isSuccess;

    }

    //删除物料信息
    @RequestMapping("/deleteDetail")
    public @ResponseBody int deleteDetail(long id){
        int isSuccess = 0;

        isSuccess = detailService.deleteDetail(id);

        return isSuccess;
    }

}
